import StorageManager from "./storageManager";
import Util from './Util';
class Bytedance {
    constructor() {
        //录屏储存
        StorageManager.getData("录屏", data => {
            if (Util.isToday(data.date)) {//是今天，不用刷新录屏次数
                this._shareVideoNumber = data.count
            } else {
                StorageManager.saveData("录屏", {
                    date: Date.now(),
                    count: 0
                });
            }
        }, () => {//第一次没有数据，初始化
            StorageManager.saveData("录屏", {
                date: 0,
                count: 0
            });
        })
    }
    /** 录屏视频位置*/
    private videoPath: string = '';
    /**分享次数*/
    private _shareVideoNumber: number = 0;
    set shareVideoNumber(value: number) {
        this._shareVideoNumber = value
        StorageManager.getData("录屏", data => {
            data.count = value
            StorageManager.saveData("录屏", value);
        })
    }
    get shareVideoNumber() {
        return this._shareVideoNumber
    }
    private time: number = 0;
    private timer: number = 0;
    /**
     * 开始录屏
     */
    recorderStart() {
        this.recorderStop();
        this.videoPath = ""
        this.time = 0;

        this.timer = setInterval(() => {
            this.time++;
        }, 1000)

        //@ts-ignore
        let recorder = tt.getGameRecorderManager();
        recorder.onStart((res) => {
            console.log(res)
        });
        recorder.onStop((res) => {
            this.videoPath = res.videoPath
        });
        recorder.onPause(() => {
            clearInterval(this.timer);
        })
        recorder.onResume(() => {
            this.timer = setInterval(() => {
                this.time++;
            }, 1000)
        })
        recorder.onError(() => {
        })
        recorder.start({ duration: 300 });
        console.log("开始录屏")
    }

    /**
     * 停止录屏
     */
    recorderStop() {
        //@ts-ignore
        tt.getGameRecorderManager().stop()
        clearInterval(this.timer);
        console.log("停止录屏")
    }

    /**
    * 暂停录屏
    */
    recorderPause() {
        //@ts-ignore
        tt.getGameRecorderManager().pause()
    }

    /**
    * 恢复录屏
    */
    recorderResume() {
        //@ts-ignore
        tt.getGameRecorderManager().resume()
    }
    /**
     * 剪辑精彩的视频片段
     * @param endTime 剪辑最后endTime秒的视频片段
     * @param shareCallFunc 分享回调函数
     * @param shareFailFunc 取消分享回调函数
     */
    clipVideo(endTime: number = 15, shareCallFunc?: Function, shareFailFunc?: Function) {
        if (this.videoPath === "") {
            //@ts-ignore
            tt.showToast({ title: "没有录屏", icon: "fail" })
            return;
        }
        //@ts-ignore
        const recorder = tt.getGameRecorderManager();
        let self = this
        recorder.clipVideo({
            path: this.videoPath,
            timeRange: [endTime, 0],
            success(res) {
                console.log("剪辑：" + res.videoPath)
                self.videoPath = res.videoPath;//生成最后endTime秒的视频
                if (shareCallFunc) {
                    self.shareVideo(shareCallFunc, shareFailFunc)
                }
            },
            fail(e) {
                console.error(e);
            },
        });
    }
    /**
     * 分享视频
     * 切记一定要停止录屏后再调用该函数，否者会调起摄像头api
     */
    shareVideo(callFunc?: Function, failFunc?: Function) {
        if (this.videoPath === "") {
            //@ts-ignore
            tt.showToast({ title: "没有录屏", icon: "fail" })
            return
        }
        if (this.time < 15) {
            //@ts-ignore
            tt.showToast({ title: "录屏时间不能少于15秒", icon: "none" });
            if (failFunc) {
                failFunc()
            }
            console.log("时间少于15")
            this.videoPath = ""
            return
        }
        console.log("开始分享")
        let self = this
        //@ts-ignore
        tt.shareAppMessage({
            channel: "video",
            title: "",
            desc: "",
            imageUrl: "",
            templateId: "", // 替换成通过审核的分享ID
            query: "",
            extra: {
                videoPath: this.videoPath, // 可替换成录屏得到的视频地址
                videoTopics: [], //该字段已经被hashtag_list代替，为保证兼容性，建议两个都填写。
                hashtag_list: [],
                video_title: "", //生成的默认内容
            },
            success() {
                if (callFunc) {
                    callFunc();
                }
                self.shareVideoNumber++;
                self.videoPath = ""
            },
            fail(e) {
                if (failFunc) {
                    failFunc();
                }
            },
        });
    }
}
export default new Bytedance();